//静态资源管理类
class StaticResoursesUtil{
    constructor(){
        this.images = new Object();
    }
    loadImage(jsonURL, callback){
        //用ajax读取文件
        let self = this;
        let xhr = new XMLHttpRequest();
        xhr.open("get",jsonURL);
        xhr.send(null);
        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4){
                if(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304){
                    let loadedNum = 0;
                    //将json转化为js对象
                    let jsonObj = JSON.parse(xhr.responseText);
                    // console.log(jsonObj);
                    for(let i = 0; i < jsonObj.images.length; ++i){
                        let img = new Image();
                        img.src = jsonObj.images[i].src;
                        img.onload = function(){
                            //加载好的图片数量加一
                            loadedNum++;
                            //保存在自己的images属性里
                            // console.log(self);
                            self.images[jsonObj.images[i].name] = img;
                            // console.log(self.images);
                            callback(loadedNum,jsonObj.images.length,self.images)
                        }
                    }

                }
            }
        }
        

    }
}